/*
 * timer_thread_handler.c
 *
 *  Created on: 2009-4-15
 *      Author: tian
 */

#include"tcpipsim.h"
#include<time.h>
#include<errno.h>

void timer_thread_handler(){
	int rv,i;
		struct timespec req,rem,rem2;
		struct tcpcb *p,*q;
		memset(&req,0,sizeof(struct timespec));
		memset(&rem,0,sizeof(struct timespec));
		memset(&rem2,0,sizeof(struct timespec));
		req.tv_nsec=500000000;
		while(1){
			rv=nanosleep(&req,&rem);
			while(rv<0&&errno==EINTR){
				rv=nanosleep(&rem,&rem2);
				rem=rem2;
			}
			if(pthread_mutex_lock(&mutex)!=0){
				fprintf(stderr,"mutex lock error in network thread\n");
			}
			for(p=head;p!=NULL;p=q){
				q=p->next;
				for(i=0;i<1;i++){
					if(p!=NULL){
						if(p->timer[i]>0)
							p->timer[i]--;
						if(p->timer[i]==0)
							tcp_timer(p,i);
					}
				}
			}
			if(pthread_mutex_unlock(&mutex)!=0){
				fprintf(stderr,"mutex unlock error in network thread\n");
			}

		}
}
